<?php
class M_upload extends CI_Model{
	var $max_idle_time = 300;
	var $user_table = "member";
	var $file_table = "upfile";
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function is_logged_in(){
		$last_activity = $this->session->userdata('last_activity');
		$logged_in = $this->session->userdata('logged_in');
		$user = $this->session->userdata('user');
		if ( ($logged_in == 'yes') && ((time() - $last_activity) < $this->max_idle_time )) {
			$this->allow_pass($user);
			return true;
		} else {
			$this->remove_pass();
			return false;
		}
	}
	
	function allow_pass($user_data) {
		$this->session->set_userdata( array( 'last_activity' => time(), 'logged_in' => 'yes', 'user' => $user_data ) );
	}
	
	function remove_pass() {
		$array_items = array('last_activity' => '', 'logged_in' => '', 'user' => '');
		$this->session->unset_userdata($array_items);
	}
	
	function get_by_username($username) {
		$query = $this->db->get_where($this->user_table, array('username' => $username), 1);
		if( $query->num_rows() > 0 ) return $query->row_array();
		return false;
	}
	
	function check_password($password,$hashed_password) {
		if($hashed_password == md5($password)) return true;
		return false;
	}
	
	function insert_file_info($fileinfo){
		$this->db->set('filename',$fileinfo['filename']);
		$this->db->set('filepath',$fileinfo['filepath']);
		$this->db->set('filetype',$fileinfo['filetype']);
		$this->db->set('filesize',$fileinfo['filesize']);
		$this->db->set('upauthor',$fileinfo['upauthor']);
		$this->db->set('checknum',$fileinfo['checknum']);
		$this->db->set('ispublic',$fileinfo['ispublic']);
		$this->db->set('uptime',$fileinfo['uptime']);
	    $this->db->insert($this->file_table);
		$res = $this->db->affected_rows();
		if($res){
			return true;
		}else{
			return false;
		}
	}
	
	function get_file_num($username){
		$this->db->select('filename,filetype,ispublic,uptime');
		$this->db->where('upauthor',$username);
		$query = $this->db->get($this->file_table);
		$rows = $query->num_rows();
		return $rows;
	}
	
	function get_user_file($username,$perpage,$offset){
		$this->db->select('filename,filetype,ispublic,uptime,upauthor');
		$this->db->where('upauthor',$username);
		$this->db->limit($perpage,$offset);
		$query = $this->db->get($this->file_table);
		$res = $query->result_array();
		return $res;
	}
	
	function drop_user_file($filename,$username){
		$query = $this->db->get_where($this->file_table,array('filename'=>$filename,'upauthor'=>$username),1);
		$res = $query->num_rows();
		$item = $query->row_array();
		if($res){
			$this->db->delete($this->file_table, array('id' => $item['id']));
			if($this->db->affected_rows()){
				return $fileinfo = array("filepath"=>$item['filepath'],"filesize"=>$item['filesize']);
			}else{
				return false;
			}			
		}
	}
	function calcu_space($param,$spacesize,$username){
		if($param == 0){
			$query = $this->db->get_where($this->user_table,array('username'=>$username),1);
			$userinfo = $query->row_array();
			$usedspace = $userinfo['userspace']+$spacesize;
			if($usedspace >= 200*1024){
				return 0;
			}
			$data = array('userspace'=>$usedspace);
			$this->db->where('username',$username);
			$this->db->update($this->user_table,$data);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 2;
			}
		}else{
			$query = $this->db->get_where($this->user_table,array('username'=>$username),1);
			$userinfo = $query->row_array();
			$usedspace = $userinfo['userspace']-$spacesize;
			$data = array('userspace'=>$usedspace);
			$this->db->where('username',$username);
			$this->db->update($this->user_table,$data);
			if($this->db->affected_rows()){
				return true;
			}else{
				return false;
			}
		}
	}
	function get_file_bycode($filecode){
		$query = $this->db->get_where($this->file_table,array('checknum'=>$filecode),1);
		$res = $query->row_array();
		if($res){
			return $res;
		}else{
			return false;
		}
	}
	function get_file_bytype($filetype){
		$query = $this->db->get_where($this->file_table,array('filetype'=>$filetype,'ispublic'=>1));
		$res = $query->row_array();
		if($res){
			return $res;
		}else{
			return false;
		}
	}
}
?>